/*
 ============================================================================
 Name        : channel_aud.c
 Author      : chenf
 Version     : 1.0
 Copyright   : loongsec
 Description : channel_add in C, Ansi-style
 ============================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sqlite3.h"
#include "cgi.h"
#include "cookie.h"

int main(void) {
        sqlite3 *db = NULL;
        int  nrow=0,ncolumn=0;
        char **azResult = NULL;
        char *zErrMsg =0;
        char sql[1000];
        char *input;
        char *req_method;
        int  rc;

        if(isvalid_cookie()!=0){
            return 0;
        }

        rc = sqlite3_open(PATH_DATABASE,&db);

        printf("Content-type:text/json;charset=utf-8\n\n");

        if (rc) {
            fprintf(stderr,"Can't open database:%s\n",sqlite3_errmsg(db));
            sqlite3_close(db);
            return 0;

        } else {
            req_method = getenv("REQUEST_METHOD");

            FILE* fp = (FILE*) stdin;
            input = getcgidata(fp, req_method);

            //PARAM un = getParam(input, "username");
            //printf("Your find is %s = %s  \n", un.key, un.value );
            PARAM method  = getParam(input, "method");

            PARAM channel_index                 = getParam(input, "channelIndex");
            PARAM channel_ctrl_signal_source    = getParam(input, "channelCtrlSignalSource");
            PARAM automatic_flash_channel_state = getParam(input, "automaticFlashChannelState");
            PARAM channel_ctrl_type             = getParam(input, "channelCtrlType");

            char last_modified[]          = {"datetime(CURRENT_TIMESTAMP,'localtime')"};


            if( strcmp(method.value, "add") ==0 ){
                sprintf(sql, "INSERT INTO [TB_CHANNEL_PARM] (\
                [channel_index],\
                [channel_ctrl_signal_source],\
                [automatic_flash_channel_state], \
                [channel_ctrl_type], \
                [last_modified]) \
                VALUES (%s, %s, %s, %s, %s);",
                channel_index.value,
                channel_ctrl_signal_source.value,
                automatic_flash_channel_state.value,
                channel_ctrl_type.value,
                last_modified);

            } else if( strcmp(method.value, "update") ==0 ){
                sprintf(sql, " UPDATE [TB_CHANNEL_PARM]  \
                set [channel_ctrl_signal_source]     = %s,\
                  [automatic_flash_channel_state]  = %s,\
                  [channel_ctrl_type]      = %s,\
                  [last_modified]          = %s \
                WHERE [channel_index]          = %s ;",
                channel_ctrl_signal_source.value,
                automatic_flash_channel_state.value,
                channel_ctrl_type.value,
                last_modified,
                channel_index.value);

            } else if( strcmp(method.value, "delete") ==0 ){
                sprintf(sql, " DELETE FROM [TB_CHANNEL_PARM]  \
                WHERE [CHANNEL_INDEX]          = %s ;",
                channel_index.value);

            }



            //printf("SQL: %s", sql);

            int sr = sqlite3_exec(db, sql, 0, 0, &zErrMsg);
            printf("{\"res\":\"%d\",\"msg\":\"%s\"}", sr, sqlite3_errmsg(db));

            sqlite3_close(db);//关闭数据库
            return 0;

        }
        fflush(stdout);

    return EXIT_SUCCESS;
}
